package com.lei.view.bill;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.GCMParameterSpec;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.lei.dao.bill.ImageDao;
import com.lei.emun.FilePathEnum;
import com.lei.ife.bill.ReSetValueInterface;
import com.lei.modal.bill.Image;
import com.lei.util.FrameUtil;

public class ImageChoose extends JPanel{
	private static final long serialVersionUID = 1L;
	private static int width = 500,height=500;
	private  ImageDao imagedao = new ImageDao();
	private GridLayout glt;
    private List<Image> imgList;
    private List<ImageIcon> icons;
    private Object obj;
    private  JFrame jframe;
    	
	public ImageChoose(Object obj) {
		this.obj = obj;
		glt = new GridLayout(0,4);
		 setLayout(glt); //
		 setFont(new Font("Helvetica", Font.PLAIN, 14));
		 initImage();
		 
         jframe = new JFrame();
         jframe.setMinimumSize(new Dimension(1000, 800));
         jframe.add(this);
         jframe.setSize(width, height);
         this.setBounds(0, 0, width, height);
         jframe.setVisible(true);
         
		 }
		 public static void main(String args[]) {
		   ImageChoose f = new ImageChoose(new Object());
		 }
		 
   public void initImage(){
	   icons = new ArrayList<ImageIcon>();
	   imgList = imagedao.getAllImages();
	   
	   for(int i = 0;i<imgList.size();i++){
		   //icons.add(new ImageIcon((imgList.get(i).getImageaddr())));
/*	   try {
			icons.add(new ImageIcon(FrameUtil.getImage((imgList.get(i).getImageaddr()))));
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		   icons.add(FrameUtil.getImage(FilePathEnum.SMALLCOMPRESSION, "88.png"));
	   }
	   
	   for(int i = 0;i<imgList.size();i++){
		   JButton jb = new JButton(icons.get(i));
		   
		   jb.setName(i + "");
		   jb.addActionListener(new MyListenner());
		   
		   this.add(jb);
	   }
   }
   
   public void MySetVisible(boolean vis){
	   
	   this.setVisible(vis);
	   this.removeAll();
	   jframe.removeAll();
	   
	   imgList = null;
	   icons = null;
   }
	
   
  class  MyListenner implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton jb = (JButton)e.getSource();
		int id = Integer.parseInt(jb.getName());
		ReSetValueInterface ret = (ReSetValueInterface)obj;
		ret.setObject(imgList.get(id));
		jframe.setVisible(false);MySetVisible(false);
	}
	  
  }
	
	
}
